package com.richapp.launcher.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.richapp.launcher.log.LogService;
import com.richapp.launcher.service.BackService;
import com.richapp.launcher.service.BoxService;
import com.richapp.launcher.service.CameraService;
import com.richapp.launcher.service.GatewayService;
import com.richapp.launcher.service.GetuiIntentService;
import com.richapp.launcher.service.GetuiService;
import com.richapp.launcher.service.InfraredService;
import com.richapp.launcher.service.RemoteService;
import com.richapp.launcher.service.SceneService;
import com.richapp.launcher.service.UdpService;
import com.richapp.launcher.service.UserService;

import java.util.List;

/**
 * Created by jack on 15/10/29.
 */
public class ServiceUtil {

    private static int count = 0;

    private ServiceUtil() {

    }

    public static void startService(Context context) {

//        //启动个推服务
//        if (!Tools.isServiceRun(context, GetuiService.class.getName())) {
//            Intent getuiService = new Intent(context, GetuiService.class);
//            context.startService(getuiService);
//            // com.getui.demo.DemoPushService 为第三方自定义推送服务
//           // PushManager.getInstance().initialize(context.getApplicationContext(), GetuiService.class);
//        }

        //启动个推服务
        if (!Tools.isServiceRun(context, GetuiIntentService.class.getName())) {
            Intent getuiService = new Intent(context, GetuiIntentService.class);
            context.startService(getuiService);

        }

        //启动日志记录服务
        if (!Tools.isServiceRun(context, LogService.class.getName())) {
            Intent logService = new Intent(context, LogService.class);
            context.startService(logService);
        }

        //启动服务
        if (!Tools.isServiceRun(context, BackService.class.getName())) {
            Intent backService = new Intent(context, BackService.class);
            context.startService(backService);
        }

        //启动用户服务
        if (!Tools.isServiceRun(context, UserService.class.getName())) {
            Intent userService = new Intent(context, UserService.class);
            context.startService(userService);
        }

        //启动BOX服务
        if (!Tools.isServiceRun(context, BoxService.class.getName())) {
            Intent boxService = new Intent(context, BoxService.class);
            context.startService(boxService);
        }

        //启动UDP服务
        if (!Tools.isServiceRun(context, UdpService.class.getName())) {
            Intent udpService = new Intent(context, UdpService.class);
            context.startService(udpService);
        }

        //启动外网服务
        if (!Tools.isServiceRun(context, RemoteService.class.getName())) {
            Intent remoteService = new Intent(context, RemoteService.class);
            context.startService(remoteService);
        }

        //启动网关服务
        if (!Tools.isServiceRun(context, GatewayService.class.getName())) {
            Intent gatewayService = new Intent(context, GatewayService.class);
            context.startService(gatewayService);
        }

        //启动红外服务
        if (!Tools.isServiceRun(context, InfraredService.class.getName())) {
            Intent infraredService = new Intent(context, InfraredService.class);
            context.startService(infraredService);
        }

        //启动摄像头服务
        if (!Tools.isServiceRun(context, CameraService.class.getName())) {
            Intent cameraService = new Intent(context, CameraService.class);
            context.startService(cameraService);
        }

        //启动情景服务
        if (!Tools.isServiceRun(context, SceneService.class.getName())) {
            Intent sceneService = new Intent(context, SceneService.class);
            context.startService(sceneService);
        }

        //启动守护服务




        //广告更新服务
        /*if (!Tools.isServiceRun(context, AdvService.class.getName())) {
        //  context.startService(new Intent(context, AdvService.class));
        }*/

        /*//红外更新服务
        if (!Tools.isServiceRun(context, InfraredUpdateService.class.getName())) {
            context.startService(new Intent(context, InfraredUpdateService.class));
        }*/

        if (count < 1) {
            Toast.makeText(context, "智能家居服务启动", Toast.LENGTH_LONG).show();
            count++;
        }

    }

    /**
     * 检测服务是否启动
     *
     * @param context
     * @param classA
     * @return
     */
    public static boolean isServiceRun(Context context, Class classA) {
        boolean flag = false;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceInfoList = activityManager.getRunningServices(30);
        if (serviceInfoList.size() > 0) {
            for (ActivityManager.RunningServiceInfo ar : serviceInfoList) {
                if (ar.service.getClassName().equals(classA.getName())) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    /**
     * 启动service
     *
     * @param context
     * @param classA
     */
    public static void startService(Context context, Class classA) {
        if (!isServiceRun(context, classA)) {
            Intent service = new Intent(context, classA);
            context.startService(service);
        }
    }

}
